package com.hyq.learn.api.config.datasource;

import com.hyq.learn.api.enumtype.DatasourceType;

/**
 * @author heyuqin
 * @date 2024/5/29 19:10
 */
public class DatasourceContextHolder {
    private static final ThreadLocal<DatasourceType> DATASOURCE_CONTEXT_HOLDER = new ThreadLocal<>();

    /**
     * 设置数据库类型
     *
     * @param datasourceType
     */
    public static void setDatasourceType(DatasourceType datasourceType) {
        if (datasourceType == null) {
            throw new NullPointerException();
        }
        DATASOURCE_CONTEXT_HOLDER.set(datasourceType);
    }

    /**
     * 获取数据库类型
     *
     * @return
     */
    public static DatasourceType getDatasourceType() {
        return DATASOURCE_CONTEXT_HOLDER.get();
    }

    /**
     * 移除数据库类型
     */
    public static void remove() {
        DATASOURCE_CONTEXT_HOLDER.remove();
    }
}
